# -*- coding: utf-8 -*-
"""
Created on Fri Jun  2 23:04:10 2017

@author: claude
"""
import re
import os
import sys
import pymongo

reload(sys) 
sys.setdefaultencoding( "utf-8" ) 

DESCRP_PATH = "./descriptions"

class MongoClint():
    collection_item = 'searches'
    def __init__(self):
        self.client = pymongo.MongoClient('localhost', 27017)
        self.db = self.client.xiaohongshu
        
    def check_item_in_db(self, keyword):
        return self.db[self.collection_item].find_one({"keyword":keyword})!=None
        
    def get_item_in_db(self, keyword):
        return self.db[self.collection_item].find_one({"keyword":keyword})
        
    def save_res_to_db(self, keywords, id_array):
        dict_item = {}
        dict_item["keyword"] = keywords
        dict_item["ids"] = list(set(id_array))
        try:
            self.db[self.collection_item].insert(dict_item)
        except pymongo.errors.DuplicateKeyError:
            print '_id %s has been existed', dict_item["_id"]
            #logger.debug('_id %s has been existed', dict_item["_id"]) 

def RemoveEmoji(s):
    """
    Remove moji
    """
    return re.sub(r'\[.*\]','',s)
    
def output_format(dict_item):
    text = str(dict_item['user']['nickname']) + "\n : " 
    text += " 喜欢: "+ str(dict_item["likes"]) + "\n" 
    text += str(dict_item['desc']) + "\n" 
    text = RemoveEmoji(text)
    text +=  "!"+"[]"+"(" + dict_item['images_list'][0]['url'] +")\n"  
    return [dict_item["oid"], text]
    
def search_by_word(mongodb, keyword):
    if mongodb.check_item_in_db(keyword)==False:
        print "From Web: ", keyword
        os.system("scrapy crawl search3 -a keywords=\""+keyword+"\" ")
    else:
        print "From DB: ", keyword
    res = mongodb.get_item_in_db(keyword)
    if not os.path.exists(DESCRP_PATH):
        os.makedirs(DESCRP_PATH)
    if os.path.isfile(DESCRP_PATH+"/"+keyword+'.txt'):
        exit
    with open(DESCRP_PATH+"/"+keyword+'.txt', 'w') as f:
        for x in res["data"]:
            [item_id, text] = output_format(x)
            f.write(text+'\n')
            
if __name__=="__main__":
    """
    support only 1 keyword
    """
    if len(sys.argv)==1:
        keywords = ["refa"]
    elif len(sys.argv)==2:
        keywords = [sys.argv[1]]
    else:
        keywords = sys.argv[1:]
    
    mongodb = MongoClint()
    for keyword in keywords:
        print keyword
        search_by_word(mongodb, keyword)


        